<!DOCTYPE html>
<html lang="zh-cn">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>发布订阅模式</title>
</head>

<body>
  <script>
    // 事件触发器
    class EventEmitter {
      constructor() {
        // { 'click': [fn1, fn2], 'change': [fn]}
        this.subs = Object.create(null)
      }
      // 注册事件
      $on(eventType, handler) {
        this.subs[eventType] = this.subs[eventType] || []
        this.subs[eventType].push(handler)
      }
      // 触发事件
      $emit(eventType, ...args) {
        if(this.subs[eventType]){
          this.subs[eventType].forEach(handler => {
            handler(...args)
          })
        }
      }
    }

    // 测试
    let em = new EventEmitter()
    em.$on('change', (...args) => {
      console.log(...args)
    })
    em.$on('change', () => {
      console.log('123')
    })
    em.$emit('change', '456', '789')
  </script>
</body>

</html>